package com.BCM.controller;



import com.BCM.po.CClass;
import com.BCM.po.Grade;
import com.BCM.po.User;
import com.BCM.service.CClassService;
import com.BCM.service.GradeService;

import com.BCM.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author tanghao
 */
@Controller("grade")
public class GradeController {
    @Autowired
    private GradeService gradeService ;
    @Autowired
    private UserService userService;
    @Autowired
    private CClassService cClassService ;
    @RequestMapping(value = "/GradeList.action")
    public String GradeList(Model model){
        Grade grade=new Grade();
        List<Grade>grades=gradeService.findAllGrade();

        model.addAttribute("Grades",grades);

        List<User>users=userService.findAllUser();

        model.addAttribute("Users",users);
        return "GradeList";
    }
    @RequestMapping(value = "/deleteGrade.action")
    @ResponseBody
    public String deleteGrade(int gradesId){
        int num=gradeService.deleteGrade(gradesId);
        if(num>0){
            return "OK";
        }else{
            return "FALL";
        }
    }

    @RequestMapping("findGradeBygradesId.action")
    @ResponseBody
    public Grade findGradeBygradesId(int gradesId){
        System.out.println("findGradeBygradesId");
        Grade grade=this.gradeService.findGradeBygradesId(gradesId);
        System.out.println(grade);
        return grade;
    }




    @RequestMapping("/updateGrade.action")
    @ResponseBody
    public String updateGrade(Grade grade){
        System.out.println("进入orderGoods");
        int num=this.gradeService.updateGrade(grade);
        System.out.println("过了orderGoods");
        if(num>0){
            return "OK";
        }else {
            return "FALL";
        }
    }

    @RequestMapping("/addGrade.action")
    public String addGrade(Grade grade, Model model){
        System.out.println("进入addGrade");
        String name=grade.getUsername();
        System.out.println("name:"+name);
        List<User>StudentClass1=userService.findUserBystudentClass(grade.getStudentClass());
        int flag=0;
        for(User user:StudentClass1){
            System.out.println(user.getUsername());
            if (user.getUsername().equals(name)){

                flag=1;
                System.out.println(name+"flag:"+flag);
            }
        }
        System.out.println("isEmpty()"+gradeService.findGradeByusername(name).isEmpty());
        if(gradeService.findGradeByusername(name).isEmpty()&&flag==1){
            gradeService.save(grade);
            return "redirect:/GradeList.action";
        }else{
            model.addAttribute("message","1");
            User user=new User();
            List<User>users=userService.findAllUser();
            CClass cClass=new CClass();
            List<CClass>cClasses=cClassService.findAllCClass();
            model.addAttribute("cClasses",cClasses);
            model.addAttribute("Users",users);
            return "GradeAdd";
        }

    }


    @RequestMapping("/findGradeByusername")
    public String findGradeByusername(String username,Model model){
        System.out.println("username:"+username);
        List<Grade> grades=this.gradeService.findGradeByusername(username);
        for (Grade grade:grades){
            System.out.println("controller"+grade);
        }
        model.addAttribute("Grades",grades);
        return "GradeList";
    }



}
